using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class MsgManager
	{
		#region CONSTRUCTORS
		static MsgManager()
		{
			_data = new MsgData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static MsgData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string msgCode,  byte msgType,  byte contentType,  string content1,  string content2,  string content3,  string description)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = msgCode;
				values[1] = Util.ReplaceNullTo(msgType, DBNull.Value);
				values[2] = Util.ReplaceNullTo(contentType, DBNull.Value);
				values[3] = Util.ReplaceNullTo(content1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(content2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(content3, DBNull.Value);
				values[6] = description;
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Insert( MsgInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = info.MsgCode;
				values[1] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.Content1, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.Content2, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.Content3, DBNull.Value);
				values[6] = info.Description;
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( int msgID,  string msgCode,  byte msgType,  byte contentType,  string content1,  string content2,  string content3,  string description)
		{
			_error = "";
			try
			{
				object[] values = new object[8];
				values[0] = msgID;
				values[1] = msgCode;
				values[2] = Util.ReplaceNullTo(msgType, DBNull.Value);
				values[3] = Util.ReplaceNullTo(contentType, DBNull.Value);
				values[4] = Util.ReplaceNullTo(content1, DBNull.Value);
				values[5] = Util.ReplaceNullTo(content2, DBNull.Value);
				values[6] = Util.ReplaceNullTo(content3, DBNull.Value);
				values[7] = description;
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( MsgInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[8];
				values[0] = info.MsgID;
				values[1] = info.MsgCode;
				values[2] = Util.ReplaceNullTo(info.MsgType, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.ContentType, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.Content1, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.Content2, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.Content3, DBNull.Value);
				values[7] = info.Description;
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( int msgID)
		{
			_error = "";
			try
			{
				object[] values = { msgID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( MsgInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.MsgID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static MsgInfo Select( int msgID)
		{
			_error = "";
			try
			{
				object[] values = { msgID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				MsgInfo info = new MsgInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return MsgManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return MsgManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}